//
//  BirthdayViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/28.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "BirthdayViewController.h"
#import "MyDataViewController.h"
#import "MyCenterViewController.h"

@interface BirthdayViewController ()
@property (nonatomic,strong)UIDatePicker *DatePickerV;
@property (nonatomic,strong)UILabel *age;
@property (nonatomic,strong)UILabel *type;
@property (nonatomic,assign)NSInteger year;
@property (nonatomic,assign)NSInteger month;
@property (nonatomic,assign)NSInteger day;
@end

@implementation BirthdayViewController

//类方法，单例对象(该单例比较特殊，记得回顾)
+(instancetype)sharedMyBirthdayViewController{
    static BirthdayViewController *myBirthdayVC = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        myBirthdayVC = [[BirthdayViewController alloc]init];
    });
    return myBirthdayVC;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"设置生日";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:(UIBarButtonItemStylePlain) target:self action:@selector(saveAction)];
    
    UILabel *ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 50)];
    ageLabel.text = @"年龄";
    ageLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:ageLabel];
    
    UILabel *typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 70, 80, 50)];
    typeLabel.text = @"星座";
    typeLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:typeLabel];
    
    self.age = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-100, 20, 80, 50)];
    self.age.text = self.age0;
    self.age.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:self.age];
    
    self.type = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-200, 70, 180, 50)];
    self.type.textColor = [UIColor blackColor];
    self.type.textAlignment = NSTextAlignmentRight;
    self.type.text = self.type0;
    [self.view addSubview:self.type];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width, 50)];
    label.text = @"*选择出生日期，系统将会自动转换为年龄及星座";
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    self.DatePickerV = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 200)];
    
    self.DatePickerV.maximumDate = [NSDate date];
    
    self.DatePickerV.datePickerMode = UIDatePickerModeDate;
    
    [self.DatePickerV addTarget:self action:@selector(change) forControlEvents:(UIControlEventValueChanged)];
    
    [self.view addSubview:self.DatePickerV];
    
}

-(void)change{
    NSDate *select = [self.DatePickerV date];
    
    NSDateFormatter *format = [NSDateFormatter new];
    [format setDateFormat:@"yyyy"];
    NSString *string = [format stringFromDate:select];
    NSString *message = [NSString stringWithFormat:@"%@",string];
    self.age.text = [NSString stringWithFormat:@"%d",2016 - [message intValue]];
    self.year = [message intValue];
    
    [format setDateFormat:@"MM"];
    NSString *string2 = [format stringFromDate:select];
    self.month = [string2 intValue];
    NSLog(@"%ld",self.month);
    [format setDateFormat:@"dd"];
    NSString *string3 = [format stringFromDate:select];
    self.day = [string3 intValue];
//    NSLog(@"%@",string3);
    if ((self.month==3 && (self.day>20 && self.day<32))||(self.month == 4 &&(self.day>0 && self.day<20))){
        self.type.text =@"白羊座";
    }else if ((self.month==4 && (self.day>19 && self.day<32))||(self.month == 5 &&(self.day>0 && self.day<21))){
        self.type.text =@"金牛座";
    }else if ((self.month==5 && (self.day>20 && self.day<32))||(self.month == 6 &&(self.day>0 && self.day<22))){
        self.type.text =@"双子座";
    }else if ((self.month==6 && (self.day>21 && self.day<32))||(self.month == 7 &&(self.day>0 && self.day<23))){
        self.type.text =@"巨蟹座";
    }else if ((self.month==7 && (self.day>22 && self.day<32))||(self.month == 8 &&(self.day>0 && self.day<23))){
        self.type.text =@"狮子座";
    }else if ((self.month==8 && (self.day>22 && self.day<32))||(self.month == 9 &&(self.day>0 && self.day<23))){
        self.type.text =@"处女座";
    }else if ((self.month==9 && (self.day>22 && self.day<32))||(self.month == 10 &&(self.day>0 && self.day<24))){
        self.type.text =@"天秤座";
    }else if ((self.month==10 && (self.day>23 && self.day<32))||(self.month == 11 &&(self.day>0 && self.day<23))){
        self.type.text =@"天蝎座";
    }else if ((self.month==11 && (self.day>22 && self.day<32))||(self.month == 12 &&(self.day>0 && self.day<22))){
        self.type.text =@"射手座";
    }else if ((self.month==12 && (self.day>21 && self.day<32))||(self.month == 1 &&(self.day>0 && self.day<20))){
        self.type.text =@"摩羯座";
    }else if ((self.month==1 && (self.day>19 && self.day<32))||(self.month == 2 &&(self.day>0 && self.day<19))){
        self.type.text =@"水瓶座";
    }else if ((self.month==2 && (self.day>18 && self.day<32))||(self.month == 3 &&(self.day>0 && self.day<21))){
        self.type.text =@"双鱼座";
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)saveAction{
        NSDate *select = [self.DatePickerV date];
    
        NSDateFormatter *format = [NSDateFormatter new];
    
        [format setDateFormat:@"yyyy年MM月dd日"];
    
        NSString *string = [format stringFromDate:select];
    
        NSString *message = [NSString stringWithFormat:@"您选择的日期是%@",string];
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"日期" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *A = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

        [MyDataViewController sharedMyDataViewController].myBirthday = [NSString stringWithFormat:@"%ld年%02ld月%02ld日",self.year,self.month,self.day];
        [MyDataViewController sharedMyDataViewController].myAge = self.age.text;
        [MyDataViewController sharedMyDataViewController].myType = self.type.text;
        [[MyDataViewController sharedMyDataViewController].myDataTV reloadData];

        [self backAction];
    }];
    
    UIAlertAction *B = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];
    
        [alert addAction:A];
        [alert addAction:B];
    
        [self presentViewController:alert animated:YES completion:nil];
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
